namespace Aob.Lite.Dsl

import System

import Boo.Lang.Compiler
import Boo.Lang.Compiler.Ast
import Boo.Lang.Compiler.TypeSystem

def GetParentMacro(node as Node):
	if node.ParentNode is not null:
		return node.ParentNode.ParentNode as MacroStatement
	else:
		return null

def GetModule(macro as MacroStatement):
	parent as Node = macro
	while not parent isa Module:
		parent = parent.ParentNode
	return (parent as Module)

def AddSingleStaticMethod(classDef as ClassDefinition, name as string, parameters as ParameterDeclarationCollection, block as Block):
	method = Method(name, Modifiers: TypeMemberModifiers.Static)
	method.Body.Add(block.CloneNode())
	for p in parameters:
		method.Parameters.Add(p)
	classDef.Members.Add(method)
	
	return method

def AddSingleStaticMethod(module as Module, name as string, parameters as ParameterDeclarationCollection, statement as Statement):
	method = Method(name, Modifiers: TypeMemberModifiers.Static)
	method.Body.Add(statement.CloneNode())
	for p in parameters:
		method.Parameters.Add(p)
	module.Members.Add(method)
	
	return method

def ParametersFromMacroArguments(expressions as ExpressionCollection):
	pdc = ParameterDeclarationCollection()
	for e in expressions:
		if e isa TryCastExpression:
			parameterName = (e as TryCastExpression).Target.ToString()
			parameterType = (e as TryCastExpression).Type
		if e isa ReferenceExpression:
			parameterName = (e as ReferenceExpression).Name
			
		pdc.Add(ParameterDeclaration(parameterName, parameterType))
			
	return pdc

def ResolveMethodReference(context as CompilerContext, _namespace as IEntity, reference as ReferenceExpression):
	return context.NameResolutionService.ResolveQualifiedName(reference.ToCodeString())

def GetEnclosingTypeDefinition(member as IMember):
	return (member.DeclaringType as IInternalEntity).Node as TypeDefinition

def m(o):
	System.Windows.Forms.MessageBox.Show(o.ToString())

